﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class HotelPage_HotelChangepsw : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string ConnSql = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
        SqlConnection conn = new SqlConnection(ConnSql);
        conn.Open();		//打开连接
        string selcmd = "SELECT * FROM HotelUser WHERE account="
            + SqlFormat.ap((string)Session["userId"])
            + " AND acpassword=" + SqlFormat.ap(TextBoxOld.Text) + ";";

        SqlDataAdapter sqladp = new SqlDataAdapter();

        sqladp.SelectCommand = new SqlCommand(selcmd, conn);

        DataTable dataTable = new DataTable();

        sqladp.Fill(dataTable);

        if (TextBoxNew.Text.Length < 6||TextBoxNew.Text.Length>16)
        {
            Response.Write("<script language=javascript > alert('新密码长度必须在6到16之间！');</script>");
            conn.Close();
            return;
        }

        if (dataTable.Rows.Count != 1)
        {
            Response.Write("<script language=javascript > alert('旧密码不正确！');</script>");
            conn.Close();
            return;
        }

        if (TextBoxNew.Text!=TextBoxRenew.Text)
        {
            Response.Write("<script language=javascript > alert('两次输入的密码不相同！');</script>");
            conn.Close();
            return;
        }

        string updcmd = "UPDATE HotelUser SET acpassword=" + SqlFormat.ap(TextBoxNew.Text.ToString())
            + " WHERE account="
            + SqlFormat.ap((string)Session["userId"])
            + ";";
        sqladp.UpdateCommand = new SqlCommand(updcmd, conn);

        if (sqladp.UpdateCommand.ExecuteNonQuery() != 1)
        {
            Response.Write("<script language=javascript > alert('修改失败..');</script>");
            conn.Close();
            return;
        }

        TextBoxOld.Text = "";
        TextBoxNew.Text = "";
        TextBoxRenew.Text = "";
        Response.Write("<script language=javascript > alert('密码修改成功，请妥善保管新密码');</script>");

        conn.Close();
    }
}